package today.rocky.memory.service.impl;


import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import today.rocky.memory.domain.Member;
import today.rocky.memory.domain.MemoryRecord;
import today.rocky.memory.domain.MemoryRecordExample;
import today.rocky.memory.domain.bo.MemoryParam;
import today.rocky.memory.domain.bo.MemorySearchParam;
import today.rocky.memory.domain.dto.MemberDTO;
import today.rocky.memory.domain.dto.MemoryRecordAddDTO;
import today.rocky.memory.domain.dto.MemoryRecordDTO;
import today.rocky.memory.exception.BusinessException;
import today.rocky.memory.mapper.MemberMapper;
import today.rocky.memory.mapper.MemoryRecordMapper;
import today.rocky.memory.service.MemoryRecordService;
import today.rocky.memory.util.DateUtil;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0
 * @date 2020/5/22 16:02
 * @description
 */
@Slf4j
@Service
public class MemoryRecordServiceImpl implements MemoryRecordService {

	@Autowired
	private MemoryRecordMapper memoryRecordMapper;
	@Autowired
	private MemberMapper memberMapper;

	@Override
	public List<MemoryRecordDTO> searchMemoryList(MemorySearchParam param) {
		//需要限制数量 30
		MemoryRecordExample example = new MemoryRecordExample();
		example.createCriteria().andUserIdEqualTo(param.getUserId())
				.andCreateTimeBetween(param.getStartDate(),param.getEndDate());
		List<MemoryRecord> records = memoryRecordMapper.selectByExample(example);
		if(records.isEmpty()){
			return null;
		}
		List<MemoryRecordDTO> recordDTOS = new ArrayList<>();
		for (MemoryRecord m:records) {
			MemoryRecordDTO recordDTO = new MemoryRecordDTO();
			BeanUtils.copyProperties(m,recordDTO);
			recordDTOS.add(recordDTO);
		}
		return recordDTOS;
	}

	@Override
	public MemoryRecordDTO getMemory(MemoryParam param) throws BusinessException {
		Member member = memberMapper.selectByPrimaryKey(param.getUserId());
		if(member==null){
			throw new BusinessException("不存在该用户");
		}
		MemoryRecordExample example = new MemoryRecordExample();

		example.createCriteria().andUserIdEqualTo(param.getUserId()).andCreateTimeEqualTo(param.getDate());
		List<MemoryRecord> memoryRecords = memoryRecordMapper.selectByExample(example);
		if (memoryRecords.size()>1){
			throw new BusinessException("业务异常，存在多条记录");
		}
		if(memoryRecords.isEmpty()){
			return null;
		}else {
			MemoryRecordDTO recordDTO = new MemoryRecordDTO();
			BeanUtils.copyProperties(memoryRecords.get(0),recordDTO);
			return recordDTO;
		}
	}

	@Override
	public int saveRecord(MemoryRecordAddDTO memory) {
		MemoryRecord record = new MemoryRecord();
		BeanUtils.copyProperties(memory,record);
		record.setCreateTime(DateUtil.date2LocalDate(memory.getCreateTime()));
		System.out.println(record.getCreateTime());
		return memoryRecordMapper.insertSelective(record);
	}

	@Override
	public int saveRecord(MemoryRecordDTO memory) {
		MemoryRecord record = new MemoryRecord();
		BeanUtils.copyProperties(memory,record);

		log.info(record.toString());
		return memoryRecordMapper.insertSelective(record);
	}
}
